import { createRouter, createWebHistory } from 'vue-router';
import HomeView from '../views/HomeView.vue';
import MovieDetail from '../views/MovieDetail.vue';
import Register from "@/views/Register.vue";
import ActivateEmail from "@/views/ActivateEmail.vue";
import Login from "@/views/Login.vue";
import ResetPassword from "@/views/ResetPassword.vue";
import PasswordReset from "@/views/PasswordReset.vue";
import Personal from "@/views/Personal.vue";
import ChangePassword from "@/views/ChangePassword.vue";
import store from "@/store";
import Collect from "@/views/Collect.vue";
import MemberCard from "@/views/MemberCard.vue";
import Order from "@/views/Order.vue";
const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/movie/:id',
    name: 'MovieDetail',
    component: MovieDetail
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/activate/:uid/:token',
    name: 'ActivateEmail',
    component: ActivateEmail
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/reset_password',
    name: 'ResetPassword',
    component: ResetPassword
  },
  {
    path: '/password_reset/:uid/:token',
    name: 'PasswordReset',
    component: PasswordReset
  },
  {
    path: '/personal',
    name: 'Personal',
    component: Personal,
    meta:{
      requireLogin:true
    }
  },
  {
    path: '/change_password',
    name: 'ChangePassword',
    component: ChangePassword,
    meta:{
      requireLogin:true
    }
  },
  {
    path: '/collect',
    name: 'Collect',
    component: Collect,
    meta:{
      requireLogin:true
    }
  },
  {
    path: '/member_card',
    name: 'MemberCard',
    component: MemberCard,
    meta:{
      requireLogin:false
    }
  },
  {
    path: '/orders',
    name: 'Orders',
    component: Order,
    meta:{
      requireLogin:true
    }
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

const token = localStorage.getItem('token')
if (token){
  store.commit('setLoginStatus',true)
}
//路由导航守卫
router.beforeEach((to,from,next)=>{
  if ((store.state.isLogin) && (to.name === 'Login' || to.name === 'Register')){
    next({name:'home'})
  }else if (to.matched.some(record => record.meta.requireLogin) && !store.state.isLogin){
    next({name:'Login',query:{jump:to.path}})
  } else {
    next()
  }
})

export default router
